package daos;

import hibernate.HibernateUtil;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import negocio.Paciente;

public class PacienteDAO {

	private static PacienteDAO instancia = null;
	private static SessionFactory sf = null;

	public static PacienteDAO getInstancia(){
		if(instancia == null){
			sf = HibernateUtil.getSessionFactory();
			instancia = new PacienteDAO();
		} 
		return instancia;
	}
	
	public void guardarPaciente(Paciente p){
		Session session = sf.openSession();
		session.beginTransaction();
		session.save(p);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}

	public void eliminarPaciente(Paciente p){
		Session session = sf.openSession();
		session.beginTransaction();
		session.delete(p);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}

	public void modificarPaciente(Paciente p){
		Session session = sf.openSession();
		session.beginTransaction();
		session.merge(p);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}
	
	@SuppressWarnings("unchecked")
	public List<Paciente> obtenerPacientes(){
		Session session = sf.openSession();
		Query q = session.createQuery("from Paciente");
		List<Paciente> pacientes = q.list();
		session.close();
		return pacientes;
	}
}
